<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Email_parser {
    var $template = '';
    var $data = array();
    var $from = '';
    var $from_name = '';
    var $to = '';
    var $to_name = '';
    var $attaches = array();
    function Email_parser(){
    
    }
    
    function set_template($t){
        $this->template = $t;
    }
    function template($t){
        $this->template = $t;
    }
    
    function parse(){
        $CI = &get_instance();

        $CI->load->module_model('administrator.email_templates.email_template_model');
        $tpl = $CI->email_template_model->get_by_key($this->template);
        if(!$tpl)
            return FALSE;
        $template = $tpl->{EMAIL_TEMPLATE_BODY};
        foreach ($this->data as $key => $val){
            if (is_array($val)){
                $template = $CI->parser->_parse_pair($key, $val, $template);        
            }else{
                $template = $CI->parser->_parse_single($key, (string)$val, $template);
            }
        }
        
        $sub = $tpl->{EMAIL_TEMPLATE_SUBJECT};
        foreach ($this->data as $key => $val){
            if (is_array($val)){
                $sub = $CI->parser->_parse_pair($key, $val, $sub);        
            }else{
                $sub = $CI->parser->_parse_single($key, (string)$val, $sub);
            }
        }
        $this->subject = $sub;
        $this->body = $template;
        
        $this->parse_img();
        return TRUE;
    }
    
    function parse_img(){
        $content = $this->body;
        preg_match_all('/<img[^>]+>/i',$content, $result); 
        if(!empty($result)){
            foreach($result[0] as $img_tag){
                preg_match_all('/(src)(.?)=(.?)("[^"]*")/i',$img_tag, $attrs);
                if(!empty($attrs)){
                    $src_attr = $attrs[4][0];
                    $tmp_src_attr = '"'.hostname().str_replace(array('\'', '"'), array('',''), $src_attr).'"';
                    $content = str_replace($src_attr, $tmp_src_attr, $content);
                }
            }
        }
        $this->body = $content;
    }

    function send($debug=FALSE){
        if(!$this->parse())
            return FALSE;
        
        $CI = &get_instance();
        $CI->load->library('email');
        $CI->email->clear();
        if(empty($this->from)){
            $CI->load->module_model('administrator.parameters.parameter_model');
            $param = $CI->parameter_model->get_by_key('contact_email');
            $this->from = $param->{PARAMETER_VALUE};
        }
        
        $CI->email->from($this->from, $this->from_name);
        $CI->email->to($this->to);

        $CI->email->subject($this->subject);
        $CI->email->message($this->body);
        
        foreach($this->attaches as $a)
            $CI->email->attach($a);
        
        $res = @$CI->email->send();
        if($debug)
            echo $CI->email->print_debugger();
        return $res;
    }
    function add($key, $val){
        $this->data[$key] = $val;
    }
    
    function attach($file){
        $this->attaches[] = $file;
    }
    
    function from($f){
        $this->from = $f;
    }
    
    function from_name($fn){
        $this->from_name = $fn;
    }
    
    function to($t){
        $this->to = $t;
    }
    
    function to_name($tn){
        $this->to_name = $tn;
    }
}
?>